package org.jeecg.common.util;

import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;
import lombok.AllArgsConstructor;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.vo.LoginMember;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * lx ---- 2022年11月9日 工具类
 */
@Component
@AllArgsConstructor
public class ToolUtils {

    private final RedisUtil restUtil;

    /**
     * date类型添加指定天数
     *
     * @param date date
     * @param i    天数
     * @return 添加后的 date
     */
    public Date addDate(Date date, int i) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, i);
        date = calendar.getTime();
        return date;
    }

    public LoginMember getLoginMember(HttpServletRequest request) {
        String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
        return (LoginMember) restUtil.get(token);
    }

    /**
     * 实名认证
     *
     * @param name   姓名
     * @param idCard 身份证
     * @return boolean
     */
    public Boolean certification(String name, String idCard) {
        String AccessKey = "1934ca1130d44d5f83609492666c5942";
        String secretKey = "f3ca33c2e09e4f8b88b5d5ef2929e831";
        String path = "https://idcheckhj.api.bdymkt.com/id_name/verify";

        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
        request.setCredentials(AccessKey, secretKey);

        // 设置header参数
        request.addHeaderParameter("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

        // 设置jsonBody参数
        String jsonBody = "idcard=" + idCard + "&name=" + name;
        request.setJsonBody(jsonBody);

        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());
        ApiExplorerResponse response = client.sendRequest(request);
        System.out.println(response.getResult());
        return response.getResult().contains("核验一致");
    }

}
